package controllers

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type JobSingleExec struct {
	beego.Controller
}

func (c *JobSingleExec) Post() {
	req := new(TblJobCtl)
	rsp := &CronRsp{ErrorCode: "000", ErrorDesc: "success"}

	o := orm.NewOrm()

	if err := json.Unmarshal(c.Ctx.Input.RequestBody, req); err != nil {
		beego.Error(err, "->", string(c.Ctx.Input.RequestBody))
		rsp.ErrorCode = "001"
		rsp.ErrorDesc = err.Error()
		goto Finish
	}

	if err := o.Read(req); err != nil {
		rsp.ErrorCode = "002"
		rsp.ErrorDesc = err.Error()
		goto Finish
	}

	req.Cron = "manual execution"
	req.ExecDay = JOB_EXEC_EVERYDAY
	req.ExecTimes = JOB_EXEC_MORE

	go (*req).Run()

Finish:
	c.Data["json"] = rsp
	c.ServeJSON()
}
